package com.java110.mall.acct.cmd.payment;

import com.java110.core.factory.TencentSendMessageFactory;
import com.java110.dto.payment.NotifyPaymentOrderDto;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.intf.cart.IStoreOrderCartV1InnerServiceSMO;
import com.java110.intf.cart.IStoreOrderV1InnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentFactoryAdapt;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(path = "/mall/api")
public class WechatNativeQrcodeNotifyCmd {

    private static Logger logger = LoggerFactory.getLogger(WechatNativeQrcodePaymentCmd.class);

    private IPaymentFactoryAdapt paymentFactoryAdapt;

    @Autowired
    private IStoreOrderCartV1InnerServiceSMO storeOrderCartV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderV1InnerServiceSMO storeOrderV1InnerServiceSMOImpl;

    /**
     * <p>支付回调Api</p>
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping(path = "/notify", method = RequestMethod.POST)
    public ResponseEntity<String> notify(@RequestBody String postInfo, HttpServletRequest request) {

        ResponseEntity<String> paramOut = null;
        try {
            logger.debug("微信支付回调报文" + postInfo);
            System.out.println(postInfo);
            NotifyPaymentOrderDto notifyPaymentOrderDto = new NotifyPaymentOrderDto("992023010100000001",postInfo,"");
            paymentFactoryAdapt = ApplicationContextFactory.getBean("wechatNativeQrcodePaymentFactory", IPaymentFactoryAdapt.class);
            PaymentOrderDto paymentOrderDto = paymentFactoryAdapt.java110NotifyPayment(notifyPaymentOrderDto);
            logger.info("【支付回调响应】 响应内容：\n" + paymentOrderDto.getResponseEntity());
            if (StringUtil.isEmpty(paymentOrderDto.getOrderId())) {
                paymentOrderDto.getResponseEntity();
            }
            if (!StringUtil.isEmpty(paymentOrderDto.getOrderId())){
                StoreOrderDto storeOrderDto = new StoreOrderDto();
                storeOrderDto.setOrderId(paymentOrderDto.getOrderId());
                List<StoreOrderDto> storeOrderDtos = storeOrderV1InnerServiceSMOImpl.queryStoreOrders(storeOrderDto);
                if (storeOrderDtos.size() == 1 && "1".equals(storeOrderDtos.get(0).getState())){
                    paymentOrderDto.setAppId(notifyPaymentOrderDto.getAppId());
                    StoreOrderPo storeOrderPo = new StoreOrderPo();
                    storeOrderPo.setOrderId(paymentOrderDto.getOrderId());
                    storeOrderPo.setState(StoreOrderDto.STATE_WAIT_SEND);
                    storeOrderV1InnerServiceSMOImpl.updateStoreOrder(storeOrderPo);
                    StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
                    storeOrderCartPo.setOrderId(paymentOrderDto.getOrderId());
                    storeOrderCartPo.setState(StoreOrderCartDto.STATE_WAIT_SEND);
                    storeOrderCartV1InnerServiceSMOImpl.updateStoreOrderCart(storeOrderCartPo);
                    TencentSendMessageFactory.sendMessageForPaymentCallback(paymentOrderDto.getOrderId());
                }
            }
            paymentOrderDto.getResponseEntity();
        } catch (Exception e) {
            logger.error("网关异常", e);
            paramOut = ResultVo.error("请求发生异常，" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return paramOut;
    }

}
